package com.ruoyi.carUser.controller;

import com.ruoyi.carUser.service.ShopLoginService;
import com.ruoyi.carUser.service.ShopUserService;
import com.ruoyi.carUser.util.JwtUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.api.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserLoginController {
    @Autowired
    private ShopLoginService shopLoginService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("userSms")
    public String userSms(String phoneNumber) {
        Map sms = shopLoginService.createSms(phoneNumber);
        System.out.println(sms);
        Object o = redisTemplate.opsForValue().get(phoneNumber);
        return o==null? "failed":"success";
    }

    @RequestMapping("userLogin")
    public AjaxResult userLogin(String phoneNumber,String code){
        String token = null;
        String o = (String) redisTemplate.opsForValue().get(phoneNumber);
        User user = new User();
        user.setPhone(phoneNumber);
        User user2 = shopUserService.selectByPhone(phoneNumber);
        if(user2==null){
            return AjaxResult.error("该用户不存在");
        }
        Long userId = user2.getUserId();
        token= JwtUtil.createJWT(userId.toString());
        Map map = new HashMap();
        map.put("token",token);
        redisTemplate.delete(phoneNumber);
        return AjaxResult.success("登陆成功，并获取到token",map);
    }
}
